﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Providers
{
    public partial class ProviderList : UserControl
    {
        // Helper event related classes for raising events to users of the control

        #region Delegates

        public delegate void ProviderEventHandler(object sender, ProviderEventArgs e);

        public delegate void TestConnectionEventHandler(object sender, EventArgs e);

        #endregion

        private static readonly object EventSelectProvider = new object();
        private static readonly object EventTestConnection = new object();

        private string _serviceName = string.Empty;

        public object DataSource
        {
            get { return ProviderListGridView.DataSource; }
            set { ProviderListGridView.DataSource = value; }
        }

        public string HeaderText
        {
            get { return HeaderLiteral.Text; }
            set { HeaderLiteral.Text = value; }
        }

        public int ParentProviderCount
        {
            get
            {
                object x = ViewState["ParentProviderCount"];
                return (x == null) ? -1 : (int) x;
            }
            set { ViewState["ParentProviderCount"] = value; }
        }

        public int SelectedIndex
        {
            get { return ProviderListGridView.SelectedIndex; }
            set { ProviderListGridView.SelectedIndex = value; }
        }

        public string ServiceName
        {
            get { return _serviceName; }
            set { _serviceName = value; }
        }

        public bool IsTestable(string providerName)
        {
            if (providerName.Contains("Sql"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string GetProviderName(object ps)
        {
            return ((ProviderListItems) ps).ProviderName;
        }

        public string GetConnectionStringName(object ps)
        {
            return ((ProviderListItems) ps).ConnectionStringName;
        }

        public string GetTypeString(object ps)
        {
            return ((ProviderListItems) ps).Type;
        }

        public string GetDescription(object ps)
        {
            return ((ProviderListItems) ps).Description;
        }

        public string GetIsGroup(object ps)
        {
            return ((ProviderListItems) ps).IsGroup.ToString();
        }

        public event ProviderEventHandler SelectProvider
        {
            add { Events.AddHandler(EventSelectProvider, value); }
            remove { Events.RemoveHandler(EventSelectProvider, value); }
        }

        public event TestConnectionEventHandler TestConnection
        {
            add { Events.AddHandler(EventTestConnection, value); }
            remove { Events.RemoveHandler(EventTestConnection, value); }
        }

        public void DataBind()
        {
            ProviderListGridView.DataBind();
        }

        public string GetToolTip(string resourceName, string itemName)
        {
            var tempString = (string) GetLocalResourceObject(resourceName);
            return String.Format((string) GetGlobalResourceObject("GlobalResources", "ToolTipFormat"), tempString,
                                 itemName);
        }

        public void ProviderListGridView_DataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            // Response.Write(DataBinder.Eval(row.DataItem, "Name"));
            if (row.RowType == DataControlRowType.DataRow &&
                row.RowIndex == ProviderListGridView.SelectedIndex)
            {
                var radioButton = (RadioButton) row.FindControl("ProviderRadioButton");
                if (radioButton != null)
                {
                    radioButton.Checked = true;
                }
            }
        }

        public void ProviderRadioButton_Click(object sender, EventArgs e)
        {
            var radioButton = (RadioButton) sender;
            var row = (GridViewRow) radioButton.Parent.Parent;

            var isGroupLabel = (Label) row.FindControl("IsGroupControl");
            bool isGroup = (isGroupLabel.Text.ToLower() == "true");
            var providerArgs = new ProviderEventArgs(ServiceName, radioButton.Text, isGroup);

            var handler = (ProviderEventHandler) Events[EventSelectProvider];
            if (handler != null)
            {
                handler(sender, providerArgs);
            }
        }

        public void TestConnection_Click(object sender, EventArgs e)
        {
            var handler = (TestConnectionEventHandler) Events[EventTestConnection];
            if (handler != null)
            {
                handler(sender, e);
            }
        }

        #region Nested type: ProviderEventArgs

        public class ProviderEventArgs : EventArgs
        {
            private readonly bool _isGroup;
            private readonly string _providerName;
            private readonly string _serviceName;

            public ProviderEventArgs(string serviceName, string providerName, bool isGroup)
            {
                _serviceName = serviceName;
                _providerName = providerName;
                _isGroup = isGroup;
            }

            public string ServiceName
            {
                get { return _serviceName; }
            }

            public string ProviderName
            {
                get { return _providerName; }
            }

            public bool IsGroup
            {
                get { return _isGroup; }
            }
        }

        #endregion

        #region Nested type: ProviderListItems

        public class ProviderListItems
        {
            private readonly string _connectionStringName;
            private readonly string _description;
            private readonly bool _isGroup;
            private readonly string _providerName;
            private readonly string _type;

            public ProviderListItems(string providerName, string description, string connectionStringName, string type,
                                     bool isGroup)
            {
                _providerName = providerName;
                _connectionStringName = connectionStringName;
                _description = description;
                _type = type;
                _isGroup = isGroup;
            }

            public string ProviderName
            {
                get { return _providerName; }
            }

            public string ConnectionStringName
            {
                get { return _connectionStringName; }
            }

            public string Description
            {
                get { return _description; }
            }

            public string Type
            {
                get { return _type; }
            }

            public bool IsGroup
            {
                get { return _isGroup; }
            }

            public override string ToString()
            {
                return _providerName;
            }
        }

        #endregion
    }
}